package com.jack.thinking.thread.core._7;

import java.lang.Thread.UncaughtExceptionHandler;

public class MultiExceptionHandler {
	public static void main(String[] args) {
		MyThreadGroup group = new MyThreadGroup("my group");
		
		MyThread myThread = new MyThread(group, "my thread");
		//myThread.setUncaughtExceptionHandler(new ObjectUncaughtExceptionHandler());
		MyThread.setDefaultUncaughtExceptionHandler(new StateUncaughtExceptionHandler());
		myThread.start();
	}
}

class MyThread extends Thread {
	private String num = "a";
	
	public MyThread(ThreadGroup group, String name) {
		super(group, name);
	}

	public MyThread() {
	}
	
	@Override
	public void run() {
		int numInt = Integer.parseInt(num);
		System.out.println("thread 's print: " + (numInt + 1));
	}
}

class MyThreadGroup extends ThreadGroup{

	public MyThreadGroup(String name) {
		super(name);
	}
	
	@Override
	public void uncaughtException(Thread t, Throwable e) {
		super.uncaughtException(t, e);
		System.out.println("thread group exception: ");
		System.out.println("group: " + e);
	}
}

class ObjectUncaughtExceptionHandler implements UncaughtExceptionHandler {

	@Override
	public void uncaughtException(Thread t, Throwable e) {
		System.out.println("object exception handler");
		System.out.println("object: " + e);
	}
	
}

class StateUncaughtExceptionHandler implements UncaughtExceptionHandler {

	@Override
	public void uncaughtException(Thread t, Throwable e) {
		System.out.println("static exception handler");
		System.out.println("static: " + e);
	}
	
}